package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.CodeTimeUtils;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.databinding.FragmentLoginPassBinding;
import com.whmnx.my.ui.fragment.SetLoginPassWordFragment;
import com.whmnx.my.ui.fragment.SetLoginWordFragment;
import com.whmnx.my.ui.fragment.SetPayWordFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/9/15 0015 11:21
 * version: 1.0
 */
public class LoginPasModel extends  ToolbarViewModel{
    //密码的绑定
    public ObservableField<String> phone = new ObservableField<>("");

    public ObservableField<String> getCode = new ObservableField<>("获取验证码");

    public ObservableField<String> code = new ObservableField<>("");

    private  SetLoginPassWordFragment mFragment;
    private  FragmentLoginPassBinding mBinding;
    private int type2;
    private String code2;


    public LoginPasModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(SetLoginPassWordFragment setLoginPassWordFragment, FragmentLoginPassBinding binding) {
        this.mFragment = setLoginPassWordFragment;
        this.mBinding = binding;
        type2 = mFragment.getArguments().getInt("type");
        if (type2 == 0){
            setTitleText("修改登录密码");
        }else {
            setTitleText("修改交易密码");
        }

        setRightIconVisible(View.VISIBLE);
        setRightIconSetVisible(View.VISIBLE);
    }


    //验证码
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (!UItils.isPhoneNumber(phone.get())){
                ToastUtils.showShort("请输入手机号！");
                return;
            }




            Map<String, Object> params = new HashMap<>();
            params.put("Mobile",phone.get());
            HttpClient.get(mFragment.getActivity(), Constants.GETCODE, params,new CallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    code2 = result;
                    CodeTimeUtils.countDowns(mBinding.tvCode);
                }
            });
        }
    });

    //下一步
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (!UItils.isPhoneNumber(phone.get())){
                ToastUtils.showShort("请输入手机号！");
                return;
            }

            if (UItils.isPhoneNumber(code.get())){
                ToastUtils.showShort("请输入验证码");
                return;
            }

            if (!code2.equals(code.get())){
                ToastUtils.showShort("输入验证码不正确");
                return;
            }

            if(type2 == 0){
                startContainerActivity(SetLoginWordFragment.class.getCanonicalName());
            }else {
                startContainerActivity(SetPayWordFragment.class.getCanonicalName());
            }
        }
    });
}
